﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Net;
using System.Text;
using PubSubWeb.Model;

namespace PubSubWeb
{
    // NOTE: If you change the class name "PubSubWeb" here, you must also update the reference to "PubSubWeb" in Web.config.
    [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PubSubWeb : IPubSubWeb
    {
        public void Subscribe(NameValueCollection formsData)
        {
            this.Subscribe(
                        formsData["hub.callback"],
                        formsData["hub.mode"],
                        formsData["hub.topic"],
                        formsData["hub.verify"],
                        formsData["hub.lease_seconds"],
                        formsData["hub.secret"],
                        formsData["hub.verify_token"]);
        }

        private void Subscribe(
                        String hubCallback, 
                        string hubMode, 
                        string hubTopic, 
                        string hubVerify, 
                        string hubLeaseSeconds, 
                        string hubSecret, 
                        string hubVerifyToken)
        {
            if (String.IsNullOrEmpty(hubCallback) || 
                String.IsNullOrEmpty(hubMode) || 
                String.IsNullOrEmpty(hubTopic) ||
                String.IsNullOrEmpty(hubVerify))
            {
                throw new InvalidOperationException();
            }

            SubscriptionMode mode = (SubscriptionMode)Enum.Parse(typeof(SubscriptionMode), hubMode);
            SubscriptionVerificationType verificationType = (SubscriptionVerificationType)Enum.Parse(typeof(SubscriptionVerificationType), hubVerify);

            if (mode == SubscriptionMode.Subscribe)
            {
                SubscriptionManager.Instance.Subscribe(
                                                hubCallback,
                                                hubTopic,
                                                verificationType,
                                                int.Parse(hubLeaseSeconds),
                                                hubSecret,
                                                hubVerifyToken);
            }
            else
            {
                SubscriptionManager.Instance.Unsubscribe(
                                                hubCallback,
                                                hubTopic,
                                                verificationType,
                                                hubSecret,
                                                hubVerifyToken);
            }
        }

        public void Publish(NameValueCollection formsData)
        {
            if (formsData["hub.mode"] != "publish")
            {
                throw new InvalidOperationException();
            }

            SubscriptionManager.Instance.Publish(formsData["hub.url"]);
        }
    }
}
